package com.finance.system.nio;

import java.nio.ByteBuffer;

public class ByteBufferUtils {
    public static byte[] readBytes(ByteBuffer buffer){
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes,0,buffer.remaining());
        return bytes;
    }

    public static byte[] readBytes(ByteBuffer buffer,int num){
        byte[] bytes = new byte[num];
        buffer.get(bytes,0,num);
        return bytes;
    }

    public static int byteArrayToInt(byte[] bytes) {
        int value = 0;
        // 由高位到低位
        for (int i = 0; i < 4; i++) {
            int shift = (4 - 1 - i) * 8;
            value += (bytes[i] & 0x000000FF) << shift;// 往高位游
        }
        return value;
    }
}
